#include "BlockTriangle.h"
#include "EtagesTriangle.h"
/*!
\brief Create a triangular block.
\param a,b,c End vertices of the block.
*/
BlockTriangle::BlockTriangle(const Vector& a,const Vector& b,const Vector& c):Triangle(a,b,c)
{
}
void BlockTriangle::Subdivide(){
	BlockTriangle btcour = clone2(0.35);
	BlockQuad * extrem[3];
	bool b= true;
	if(btcour.Area()>tailleMinimumCour){
		if(true){//mettre un random pour la division differentes des blocs de coins
			for(int t = 0 ; t<3;t++){
				int precedent = (t+2)%3;
				int suivant = (t+1) %3;
				Vector x = Intersection2(p[t], p[suivant], btcour[t], btcour[precedent]);
				Vector y = Intersection2(p[t], p[precedent], btcour[t], btcour[suivant]);
			
				
				extrem[t] = new BlockQuad( btcour[t],y,p[t],x);
				if(extrem[t]->Area()<.1){
					b = false;
				}
			}
			if(b){
				for(int t = 0 ; t<3;t++){
					EtagesQuad(extrem[t]->clone2(0.95), 1).CreerEtage();
				}
				for(int t = 0 ; t<3;t++){
					int precedent = (t+2)%3;
					int suivant = (t+1) %3;
					BlockQuad bd1 = *extrem[t];
					BlockQuad bd2 = *extrem[suivant];
					BlockQuad cv = BlockQuad(btcour[t],bd1[3],bd2[1], btcour[suivant]);
					cv.divise();
			}
		}
			
		}else{
			int i;
		}
	}else{
	//action si trop petit
		BlockTriangle bt1(p[0], p[1], (p[1]+p[2])/2.0);
		EtagesTriangle(bt1.clone2(0.95), 1).CreerEtage();
		BlockTriangle bt2(p[0], p[1], (p[1]+p[2])/2.0);
		EtagesTriangle et1 (bt2.clone2(0.95), 1) ;
		et1.CreerEtage();

	}
	
}

BlockTriangle BlockTriangle::clone(double scalaire){
	Vector p2[3];
	p2[0]= Util::getPartie(p[2], p[0], scalaire);
	p2[1]= Util::getPartie(p[2], p[1], scalaire);
	p2[2]=Util::getPartie(p[0], p[2], scalaire);
	return BlockTriangle(p2[0], p2[1], p2[2]);
}

BlockTriangle BlockTriangle::clone2(double scalaire){
	Vector intersect = Intersection2(p[0], (p[2]+p[1])/2.0,p[1], (p[2]+p[0])/2.0);
	Vector p2[3];

	p2[2]= Util::getPartie(intersect, p[2], scalaire);
	p2[0]=Util::getPartie(intersect, p[0], scalaire);
	p2[1]=Util::getPartie(intersect, p[1], scalaire);

	return BlockTriangle(p2[0], p2[1], p2[2]);
}

BlockTriangle BlockTriangle::Shrink(const double& ab,const double& bc, const double& ca)
{
  Vector sa = p[0]-Normalized((p[1]-p[0])/Vector(0,0,1))*ab;
  Vector sb = p[1]-Normalized((p[2]-p[1])/Vector(0,0,1))*bc;
  Vector sc = p[2]-Normalized((p[0]-p[2])/Vector(0,0,1))*ca;

  return BlockTriangle(
    Intersection2(sa,sa+(p[1]-p[0]),sb,sb+(p[2]-p[1])),
    Intersection2(sb,sb+(p[2]-p[1]),sc,sc+(p[0]-p[2])),
    Intersection2(sc,sc+(p[0]-p[2]),sa,sa+(p[1]-p[0])));
}
void BlockTriangle::definit(){
	int rand = Util::random();
	//if(rand<=50){
		if(true){
		Vector x = Util::getPartie(p[0], p[1], 0.3);
		Vector y = Util::getPartie(p[0], p[2], 0.3);
		BlockTriangle bt = BlockTriangle(p[0], x, y);

		bt.Jardin(0);
		//PrismTriangle(bt, 2.0).Render();


		BlockQuad bq = BlockQuad(x,y, p[2],p[1]);
		//BlockQuad bq = BlockQuad(p[2],y,x,p[1]);
		//PrismQuad(bq, 3.0).Render();
		//bq.definit();
	}/*else if(rand<=70){
		Subdivide();
	}else{
		Subdivide();
	}*/


		
}

void BlockTriangle::Jardin(int sommet){
	int precedent = (sommet+2)%3;
	int suivant = (sommet+1) %3;

	BlockTriangle bt = clone2(0.95);

	BlockQuad barriere1 = BlockQuad( bt[sommet],bt[suivant],p[suivant],p[sommet]);
	barriere1.separation();


	BlockQuad barriere2 = BlockQuad(p[precedent],p[sommet], bt[sommet],  bt[precedent]);
	barriere2.separation();
}